package com.walkthecow.justdraw;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class SaveBitmap {
	private String mFileName;
	private String mFolderName;
	private Context mContext;
	private View mView;
	private Bitmap mBitmap;
	private String mFullPath;

	public SaveBitmap(Context c, View view, String fileName, String folderName){
		mContext = c;
		mView = view;
		mFileName = fileName;
		mFolderName = folderName;
		mBitmap = Bitmap.createBitmap(mView.getWidth(), mView.getHeight(), Bitmap.Config.ARGB_8888);
	}

	public boolean saveView()
	{
		Canvas c = new Canvas(mBitmap);
		mView.draw(c);
		
		if(isSdWritable()){
			//Save to SD card
			if(saveImageToExternalStorage()){
				Toast.makeText(mContext, mContext.getText(R.string.text_file_saved_external) + " " + mFullPath + mFileName + ".png", Toast.LENGTH_SHORT).show();
				return true;
			}else{
				Toast.makeText(mContext, mContext.getText(R.string.text_file_not_saved), Toast.LENGTH_SHORT).show();
				return false;
			}
		}else{
			//Save internally
			if(saveImageToInternalStorage()){
				Toast.makeText(mContext, mContext.getText(R.string.text_file_saved_internal) + mFileName, Toast.LENGTH_SHORT).show();
				return true;
			}else{
				Toast.makeText(mContext, mContext.getText(R.string.text_file_not_saved), Toast.LENGTH_SHORT).show();
				return false;
			}
		}
	}

	//The method first checks for the file in the external storage. 
	//If the file is not there or there is no external storage it will look for the file in the internal storage.
	public boolean isSdWritable() {

		boolean mExternalStorageWriteable = false;

		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
			// We can read and write the media
			mExternalStorageWriteable = true;
			// Get the File path.
		} else {
			// Something else is wrong. It may be one of many other states, but all we need
			//  to know is we can neither read nor write
			mExternalStorageWriteable = false;
		}
		return mExternalStorageWriteable;

	}

	private boolean saveImageToExternalStorage() {
		mFullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + mFolderName;

		try {
			File dir = new File(mFullPath);
			if (!dir.exists()) {
				dir.mkdirs();
			}

			OutputStream fOut = null;
			File file = new File(mFullPath, mFileName + ".png");
			file.createNewFile();
			fOut = new FileOutputStream(file);

			// 100 means no compression, the lower you go, the stronger the compression
			mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
			fOut.flush();
			fOut.close();

			MediaStore.Images.Media.insertImage(mContext.getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());

			return true;

		} catch (Exception e) {
			Log.e("saveToExternalStorage()", e.getMessage());
			return false;
		}
	}

	private boolean saveImageToInternalStorage() {

		try {
			FileOutputStream fos = mContext.openFileOutput(mFileName, Context.MODE_PRIVATE);
			// Writing the bitmap to the output stream
			mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
			fos.close();

			return true;
		} catch (Exception e) {
			Log.e("saveToInternalStorage()", e.getMessage());
			return false;
		}
	}
}
